﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;


namespace PeppeDotNet.SP2010.Html5Fields.Controls
{
    public class Html5InputControl : HtmlInputControl, IPostBackDataHandler
    {
        public Html5InputControl(string type) : base(type) { }

        #region Events
        private static readonly object EventServerChange = new object();
        public event EventHandler ServerChange
        {
            add { base.Events.AddHandler(EventServerChange, value); }
            remove { base.Events.RemoveHandler(EventServerChange, value); }
        } 
        #endregion

        #region Properties
        public int MaxLength
        {
            get
            {
                string s = (string)this.ViewState["maxlength"];
                if (s == null) return -1;
                return int.Parse(s, CultureInfo.InvariantCulture);
            }
            set { base.Attributes["maxlength"] = value.ToString(); }
        }

        public int Size
        {
            get
            {
                string s = base.Attributes["size"];
                if (s == null) return -1;
                return int.Parse(s, CultureInfo.InvariantCulture);
            }
            set { base.Attributes["size"] = value.ToString(); }
        }

        public override string Value
        {
            get
            {
                string str = base.Attributes["value"];
                if (str == null) return string.Empty;
                return str;
            }
            set { base.Attributes["value"] = value; }
        }
        #endregion  

        protected bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            string str = this.Value;
            string str2 = postCollection.GetValues(postDataKey)[0];
            if (!str.Equals(str2))
            {
                this.Value = str2;
                return true;
            }
            return false;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);            
            if ((!base.Disabled && (base.Events[EventServerChange] == null)) || base.Type.Equals("password", StringComparison.OrdinalIgnoreCase))
            {
                this.ViewState.SetItemDirty("value", false);
            }
        }

        protected void OnServerChange(EventArgs e)
        {
            EventHandler handler = (EventHandler)base.Events[EventServerChange];
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void RaisePostDataChangedEvent()
        {
            this.OnServerChange(EventArgs.Empty);
        }

        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            return this.LoadPostData(postDataKey, postCollection);
        }

        void IPostBackDataHandler.RaisePostDataChangedEvent()
        {
            this.RaisePostDataChangedEvent();
        }     
    }
}
